//
//  XJModifyAddressViewController.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/6/29.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

#import "XJModifyAddressViewController.h"
// view
#import "XJLabelTextFieldCell.h"
#import "XJLabelLabelCell.h"
#import "XJNearByAddresNavView.h"

//controller
#import "XJNearbyAddressViewController.h"




@interface XJLabelTextFieldModel:NSObject

@property(nonatomic, copy) NSString *name;
@property(nonatomic, copy) NSString *content;
@property(nonatomic, copy) NSString *placeholder;
@end



@interface XJModifyAddressViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic, strong) UITableView *tableView;
@property(nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) UIView *footerView; //确认 6-29


/**传给服务器的请求参数*/
/**1.收货人姓名*/
@property (nonatomic, copy) NSString *name;
/**2.手机号*/
@property (nonatomic, copy) NSString *phone;
/**3.省 可能为空*/
@property (nonatomic, copy) NSString *province;
/**4.市*/
@property (nonatomic, copy) NSString *city;
/**5.district 可能为空*/
@property (nonatomic, copy) NSString *district;
/**6.门牌号*/
@property (nonatomic, copy) NSString *house_number;
/**7.详细地址*/
@property (nonatomic, copy) NSString *address_name;
/**8.*/
@property (nonatomic, copy) NSString *lng;
/**9.*/
@property (nonatomic, copy) NSString *lat;
@end

@implementation XJModifyAddressViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"新增收货地址";
    
    // 有范儿了。不给frame他能走吗
    [self.view addSubview:self.tableView];
    self.tableView.frame = CGRectMake(0, 63, KWidth, KHeight - 64);
    self.tableView.tableFooterView = self.footerView;
    [self moniData];
    
    XJLog(@"%@",self.addressM);
    
}


- (void)moniData {
    NSArray *arr = @[@{@"name":@"收货人",@"content":@"",@"placeholder":@"请输入收货人姓名"},
                     @{@"name":@"手机号",@"content":@"",@"placeholder":@"请输入手机号"},
                     @{@"name":@"地址",@"content":@"",@"placeholder":@"请选择地址"},
                     @{@"name":@"门牌号",@"content":@"",@"placeholder":@"如13号21室"},
                     ];
    
    self.dataArray = [NSMutableArray array];
    
    for (NSDictionary *dic in arr) {
        XJLabelTextFieldModel *model = [XJLabelTextFieldModel mj_objectWithKeyValues:dic];
        
        [self.dataArray addObject:model];
    }
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableView reloadData];
    });
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:(UITableViewStylePlain)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
        // 注册
        [_tableView registerClass:[XJLabelTextFieldCell class] forCellReuseIdentifier:@"XJLabelTextFieldCellId"];
        [_tableView registerClass:[XJLabelLabelCell class] forCellReuseIdentifier:@"XJLabelLabelCellId"];
    }
    return _tableView;
}

- (UIView *)footerView {
    if (!_footerView) {
        _footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KWidth, 60)];
        
        UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [button setTitle:@"确认" forState:(UIControlStateNormal)];
        [button setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        button.frame = CGRectMake(15, 10, KWidth - 30, 40);
        button.layer.masksToBounds = YES;
        button.layer.cornerRadius = 15;
        button.backgroundColor = XJCOLOR_RGB(255, 217, 68);
        [button addTarget:self action:@selector(surebuttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
        
        [_footerView addSubview:button];
    }
    return _footerView;
}

- (void)surebuttonAction:(UIButton *)button {
    XJLog(@"确认");
    
    [self requestAddNewAddress];
}
//
//- (XJAddNewAddressFooterView *)footerView {
//    if(!_footerView){
//        _footerView = [[XJAddNewAddressFooterView alloc] initWithFrame:CGRectMake(0, 0, KWidth, 100)];
//
//        XJWeakSelf(self)
//        // 添加地址回调
//        _footerView.addNewAddressFooterViewSureButtonBlock = ^{
//            [weakself surebuttonAction];
//        };
//    }
//    return _footerView;
//}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    XJLabelTextFieldModel *model = self.dataArray[indexPath.row];
    
    if (indexPath.row == 2) { // 地址cell
        XJLabelLabelCell *llCell = [tableView dequeueReusableCellWithIdentifier:@"XJLabelLabelCellId"];
        llCell.accessoryType =  UITableViewCellAccessoryDisclosureIndicator;
        
        [llCell configLabelLabelCellWithAddress:model.content];
        
        return llCell;
    }else {
        XJLabelTextFieldCell *ltcell = [tableView dequeueReusableCellWithIdentifier:@"XJLabelTextFieldCellId"];
        ltcell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [ltcell configureLabelTextFieldCellWithName:model.name contentStr:model.content placeholder:model.placeholder];
        
        XJWeakSelf(self)
        ltcell.labelTextFieldCellBlock = ^(NSString *contentStr) {
            model.content = contentStr;
            
            if (indexPath.row == 0) {
                weakself.name = contentStr;
            }else if (indexPath.row == 1){
                weakself.phone = contentStr;
            }else if (indexPath.row == 3){
                weakself.house_number = contentStr;
            }
        };
        return ltcell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 2) { // 地址
        return 80;
    } else {
        return 44;
    }
}


#pragma mark -  UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    // 回收键盘
    [self.view endEditing:YES];
    
    if (indexPath.row == 2) { // 点击地址跳转 附近地址控制器
        XJNearbyAddressViewController *nearByVC = [[XJNearbyAddressViewController alloc] init];
        
        XJLabelTextFieldModel *model = self.dataArray[indexPath.row];
        
        XJWeakSelf(self)
        // block反向传值。给对应model赋值 并刷新tableview
        nearByVC.nearbyAddressVCBlock = ^(NSString *province, NSString *district, NSString *city, NSString *address_name, NSString *lng, NSString *lat, NSString *name) {
            model.content = [NSString stringWithFormat:@"%@%@",name,address_name];
            
            weakself.address_name = address_name; // 保留一份
            weakself.province = province;
            weakself.district = district;
            weakself.city = city;
            weakself.address_name = address_name;
            weakself.lng = lng;
            weakself.lat = lat;
            
            [self.tableView reloadData];
        };
        [self.navigationController pushViewController:nearByVC animated:YES];
    }
}


#pragma mark - 接口回调

- (void)requestAddNewAddress {
    
    XJLog(@"收货人:%@",self.name);
    XJLog(@"手机号:%@",self.phone);
    XJLog(@"地址:%@",self.address_name);
    XJLog(@"门牌号:%@",self.house_number);
    
    if ([NSString isEmptyString:self.name]) {
        [JRToast showWithText:@"请输入姓名"];
        return;
    }
    if ([NSString isEmptyString:self.phone]) {
        [JRToast showWithText:@"请输入手机号"];
        return;
    }
    if (![self.phone isPhoneNumber]) {
        [JRToast showWithText:@"请输入正确的手机号"];
        return;
    }
    if ([NSString isEmptyString:self.address_name]) {
        [JRToast showWithText:@"请选择收货地址"];
        return;
    }
    if ([NSString isEmptyString:self.house_number]) {
        [JRToast showWithText:@"请输入门牌号"];
        return;
    }
    
    // 组装参数
    NSMutableDictionary *parDicM = [NSMutableDictionary dictionary];
    // 可能为空 预防雪崩
    if ([NSString isEmptyString:self.province]) {
        [parDicM setValue:@"" forKey:@"province"];
    }else {
        [parDicM setValue:self.province forKey:@"province"];
    }
    
    if ([NSString isEmptyString:self.district]) {
        [parDicM setValue:@"" forKey:@"district"];
    }else {
        [parDicM setValue:self.district forKey:@"district"];
    }
    
    [parDicM setValue:self.name forKey:@"name"];
    [parDicM setValue:self.phone forKey:@"phone"];
    [parDicM setValue:self.city forKey:@"city"];
    [parDicM setValue:self.house_number forKey:@"house_number"];
    [parDicM setValue:self.address_name forKey:@"address_name"];
    [parDicM setValue:self.lng forKey:@"lng"];
    [parDicM setValue:self.lat forKey:@"lat"];
    
    [XJHttpTool requestAddNewAddressWithDic:parDicM successH:^(id responseObjc) {
        XJLog(@"成功:%@",responseObjc);
        NSNumber *code = responseObjc[@"code"];
        if ([code isEqualToNumber:@0]) {
            [JRToast showWithText:@"收货地址添加成功"];
        }else {
            [JRToast showWithText:@"添加失败"];
        }
        
    } failH:^(NSError *error) {
        [JRToast showWithText:[error localizedDescription]];
    }];
}

- (void)dealloc {
    XJLog(@"xxxxxxx");
}
@end
